package com.coldworks.coldjoke;

import java.util.Observable;
import java.util.Observer;

import com.coldworks.base.BaseFragment;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.JokeManager.JokeManagerDelegate;
import com.coldworks.coldjoke.manager.UserManager;
import com.coldworks.coldjoke.manager.UserManager.UserStateSubject;
import com.coldworks.coldjoke.model.JokeEntity;
import com.coldworks.coldjoke.ui.JokePageView;
import com.coldworks.coldjoke.ui.JokeQuickView;
import com.coldworks.coldjoke.ui.JokePageView.JokePageViewDelegate;
import com.coldworks.coldjoke.ui.JokeQuickView.JokeQuickViewDelegate;
import com.coldworks.coldjoke.util.CONST;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationJokeFragment extends BaseFragment implements Observer,
		JokeManagerDelegate, JokePageViewDelegate, JokeQuickViewDelegate {

	private OnMenuShowListener onMenuShowListener;
	private JokePageView jokePageView;
	private JokeQuickView jokeQuickView;
	// private int location = 0;
	private int[] location = new int[3];
	private JokeEntity jokeAll;
	private JokeEntity jokeHot;
	private JokeEntity jokeShit;
	private JokeEntity jokeCurrent;
	private UserStateSubject userStateSubject;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			onMenuShowListener = (OnMenuShowListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement onMenuShowListener");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		jokeAll = new JokeEntity(getActivity(), 0, "new", null,
				CONST.TYPE.TEXT_AND_IMAGE, this);
		jokeHot = new JokeEntity(getActivity(), 1, "popular", "weekly",
				CONST.TYPE.TEXT_AND_IMAGE, this);
		jokeShit = new JokeEntity(getActivity(), 2, "shit", "weekly",
				CONST.TYPE.TEXT_AND_IMAGE, this);
		jokeCurrent = null;
		userStateSubject = UserManager.getInstance().getUserStateSubject();
		userStateSubject.addObserver(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater
				.inflate(R.layout.navigation_joke, container, false);
		if (jokeCurrent == null) {
			jokeCurrent = jokeAll;
		}
		jokePageView = (JokePageView) view.findViewById(R.id.joke_page_view);
		jokePageView.start(this, jokeCurrent);
		jokeQuickView = (JokeQuickView) view.findViewById(R.id.joke_quick_view);
		jokeQuickView.start(this, jokeAll, jokeHot, jokeShit);
		if (UserManager.getInstance().isLogin()) {
			jokePageView.setUserState(jokePageView.userStateLogin);
		} else {
			jokePageView.setUserState(jokePageView.userStateLogout);
			jokeAll.manager.removeLikeState();
			jokeHot.manager.removeLikeState();
			jokeShit.manager.removeLikeState();
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("location", location[jokeCurrent.tag]);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CONST.RESULT.LOGIN_RESULT:
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable o, Object data) {
		UserStateSubject observable = (UserStateSubject) o;
		if (observable.getUserState() == UserManager.LOGINED) {
			log.info("login notify");
			jokePageView.setUserState(jokePageView.userStateLogin);
		} else {
			log.info("logout notify");
			jokePageView.setUserState(jokePageView.userStateLogout);
			jokeAll.manager.removeLikeState();
			jokeHot.manager.removeLikeState();
			jokeShit.manager.removeLikeState();
		}
	}

	@Override
	public void JMloadNew(JokeEntity jokeEntity, int newCount) {
		// TODO:隐藏初始化loading窗口
		jokeQuickView.doneNew(jokeEntity, newCount);
		jokePageView.doneNew(jokeEntity, newCount);
	}

	@Override
	public void JMloadNext(JokeEntity jokeEntity, int nextCount) {
		// TODO:隐藏初始化loading窗口
		jokeQuickView.doneNext(jokeEntity, nextCount);
		jokePageView.doneNext(jokeEntity, nextCount);
	}

	@Override
	public void JMloadNewAndNext(JokeEntity jokeEntity, int newCount,
			int nextCount) {
		// TODO:隐藏初始化loading窗口
		jokeQuickView.doneNewAndNext(jokeEntity, newCount, nextCount);
		jokePageView.doneNewAndNext(jokeEntity, newCount, nextCount);
	}

	@Override
	public void JQVgetNew() {
		jokeCurrent.manager.getNew();
	}

	@Override
	public void JQVgetNext() {
		jokeCurrent.manager.getNext();
	}

	@Override
	public void JQVgetNewAndNext() {
		jokeCurrent.manager.getNewAndNext();
	}

	@Override
	public void JQVselect(JokeEntity mJokeEntity) {
		jokePageView.reload(mJokeEntity);
		jokePageView.zoomOut();
		jokeQuickView.zoomOut();
	}

	@Override
	public void JQVshowView() {
		jokePageView.zoomOut();
		jokeQuickView.zoomOut();
	}

	@Override
	public void JQVshowMenu() {
		onMenuShowListener.showMenuNavigation();
	}

	@Override
	public void JPVselect(JokeEntity jokeEntity) {
		jokeQuickView.reload(jokeEntity);
	}

	@Override
	public void JPVshowList(JokeEntity mJokeEntity) {
		jokeQuickView.reload(mJokeEntity);
		jokePageView.zoomIn();
		jokeQuickView.zoomIn();
	}

	@Override
	public void JPVlogin() {
		onMenuShowListener.showMenuLogin();
	}

}
